package com.guli.eduCrm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.eduCrm.entity.CrmBanner;
import com.guli.eduCrm.mapper.CrmBannerMapper;
import com.guli.eduCrm.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author zxc
 * @since 2023-04-11
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {
    /**
     * 具体实现 分页显示所查询到的数据;
     * @param bannerPage
     */
    @Override
    public void conditionQueryPageBanner(Page<CrmBanner> bannerPage) {
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        // TODO 根据前端所传递过来的查询条件，对于该查询条件进行封装;（即，此处queryWrapper对象中所封装的查询条件是静态的，不可变的;）
        this.page(bannerPage , queryWrapper);
    }

    @Override
    @Cacheable(value = "banner" , key = "'indexBannerList'")
    public List<CrmBanner> queryCountBanner(Integer count) {
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        // （1）根据id，降序排序;
        queryWrapper.orderByDesc("id");
        // （2）限制所能够查询到的记录总数;
        // queryWrapper.last，即用来拼接sql语句
        queryWrapper.last("limit " + count);
        List<CrmBanner> result = this.list(queryWrapper);
        return result;
    }

}
